package com.atguigu.flink.window;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.function.WaterSensorMapFunction;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class WatermarkPass2 {
    public static void main(String[] args) throws Exception {
        // TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // TODO 2.执行并行度
        env.setParallelism(1);
        // TODO 3.从指定端口读取数据
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);
        // TODO 5.对流中的数据进行类型转换
        SingleOutputStreamOperator<WaterSensor> mapDS = socketDS.map(new WaterSensorMapFunction());
        // TODO 4.指定Watermark的生成策略以及提取事件时间字段
        SingleOutputStreamOperator<WaterSensor> withWatermarkDS = mapDS.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<WaterSensor>forMonotonousTimestamps()
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<WaterSensor>() {
                                    @Override
                                    public long extractTimestamp(WaterSensor waterSensor, long l) {
                                        return waterSensor.getTs();
                                    }
                                }
                                )
        );
        // TODO 6.分组
        KeyedStream<WaterSensor, String> keyByDS = withWatermarkDS.keyBy(WaterSensor::getId);
        // TODO 7.开窗
        WindowedStream<WaterSensor, String, TimeWindow> windowDS = keyByDS.window(TumblingEventTimeWindows.of(Time.milliseconds(10)));
        // TODO 8.对窗口数据进行处理
        SingleOutputStreamOperator<String> processDS = windowDS.process(
                new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void process(String s, ProcessWindowFunction<WaterSensor, String, String, TimeWindow>.Context context, Iterable<WaterSensor> iterable, Collector<String> collector) throws Exception {
                        String windowStart = DateFormatUtils.format(context.window().getStart(), "yyyy-MM-dd HH:mm:ss.SSS");
                        String windowEnd = DateFormatUtils.format(context.window().getEnd(), "yyyy-MM-dd HH:mm:ss.SSS");
                        long count = iterable.spliterator().estimateSize();
                    }
                }
        );
        // TODO 9.打印
        processDS.print();
        // TODO 10.提交作业
        env.execute();
    }
}


// TODO 1.指定流处理环境
// TODO 2.执行并行度
// TODO 3.从指定端口读取数据
// TODO 4.指定Watermark的生成策略以及提取事件时间字段
// TODO 5.对流中的数据进行类型转换
// TODO 6.分组
// TODO 7.开窗
// TODO 8.对窗口数据进行处理
// TODO 9.打印
// TODO 10.提交作业